package com.gui.registration.controller;

import com.gui.entity.R;
import com.gui.hospital.model.Hospital;
import com.gui.registration.model.RegistrationRecord;
import com.gui.registration.service.RegistrationService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 言叶
 * Date：2025/9/3
 */
@RestController
@RequestMapping("/registration")
@AllArgsConstructor
@CrossOrigin
public class RegistrationController {
    private RegistrationService registrationService;

    /*
     * 根据名称查询医院
     * @param name
     * @return
     */
    @GetMapping("/getHospitalByName")
    public R<List<Hospital>> getHospitalByName(@RequestParam(value = "name", required = false) String name) {
        List<Hospital> hospital = registrationService.getHospitalByName(name);
        return new R<>(hospital);
    }
    /*
     *挂号 用户id 患者id 号源id
     */
    @PostMapping("/registration")
    public R registration(@RequestBody RegistrationRecord  registration) {

        Boolean result = registrationService.addRegistration(registration);

        return result ? R.success("挂号成功") : R.fail("挂号失败");
    }
    /*
     * 查询挂号记录，根据用户id user_id
     */
    @GetMapping("/getRegistration/{userId}")
    public R<List<RegistrationRecord>> getRegistration(@PathVariable("userId") Integer useId) {
        List<RegistrationRecord> registration = registrationService.getByUserId(useId);
        return new R<>(registration);
    }
    /*
     * 删除挂号记录，需要释放号源，更改号源状态
     * 订单id
     */
    @DeleteMapping("/deleteRegistration/{orderId}")
    public R deleteRegistration(@PathVariable("orderId") String orderId) {
        Boolean result = registrationService.cancelOrder(orderId);
        return result ? R.success("取消成功") : R.fail("取消失败");
    }
    /*
     * 修改挂号记录，更新订单状态
     * 订单id
     */
    @PutMapping("/updateRegistration")
    public boolean updateRegistration(@RequestBody RegistrationRecord record) {
        return registrationService.updateByOrderId(record);
    }
}